package vcp.client.GUI.mvc.controller;

import org.joda.time.DateTime;

import vcp.client.GUI.events.ValidationDescriptionEvent;
import vcp.client.GUI.mvc.model.UIAbstractModel;
import vcp.client.GUI.mvc.model.UICheckInModel;
import vcp.client.GUI.mvc.view.UIAbstractView;
import vcp.client.GUI.mvc.view.UIRegularCheckInView;
import vcp.client.interfaces.listeners.IValidationListener;
import vcp.client.utility.Common.UserNotification;
import vcp.client.utility.UtilityHelper;
import vcp.entity.Subscription;
import vcp.enums.PopUpMessageType;
import vcp.enums.Screen;

/**
 * 
 * @author VCP team
 *         <p>
 *         regular check in controller
 *         </p>
 * 
 */
public class UIRegularCheckInController extends UIAbstractController implements IValidationListener
{
	/**
	 * Application online data source
	 */
	private final UICheckInModel model;

	/**
	 * Application online panel
	 */
	private final UIRegularCheckInView view;

	/**
	 * 
	 * @param model
	 *            : Online data source
	 * @param view
	 *            : Online GUI panel
	 */
	public UIRegularCheckInController ( UIRegularCheckInView view, UICheckInModel model )
	{
		super(Screen.REGULAR_CHECK_IN);
		this.model = model;
		this.view = view;
		this.setupListeners();
	}

	// ****************************************************************************************************************************
	// UIAbstractController methods ***
	// ****************************************************************************************************************************
	@Override
	public UIAbstractView getView ()
	{
		return this.view;
	}

	@Override
	public UIAbstractModel getModel ()
	{
		return this.model;
	}

	@Override
	protected void setupListeners ()
	{
		super.setupListeners();
		this.view.setValidationListener(this);
	}

	@Override
	public void uploadDataContext ( Object dataContext )
	{
		if ( dataContext instanceof Subscription )
		{
			Subscription subscriptions = (Subscription) dataContext;
			this.model.setSubscription(subscriptions);
			this.view.setSiteName(this.model.getSubscription().getSite().toString());
			this.view.updateDateTimes(this.parentDelegate.requestCurrentClientDateTime());
		}
	}

	// ****************************************************************************************************************************
	// IValidationListener methods ***
	// ****************************************************************************************************************************
	@Override
	public void onValidationRequest ( ValidationDescriptionEvent validationEvent )
	{
		final DateTime date = validationEvent.getDate();
		final String email = validationEvent.geteMail();

		if ( UtilityHelper.getUtilityHelperInstance().isParamsNotNull(date, email) )
		{
			this.model.verifyRegularCheckInInfo(date, email);
		}
		else
		{
			this.parentDelegate.showPopUpMessage(UserNotification.WRONG_DATA, PopUpMessageType.ERROR);
		}
	}
}
